package com.atguigu.srb.handler;

import com.atguigu.srb.exception.SRBException;
import com.atguigu.srb.result.ResponseEnum;
import com.atguigu.srb.result.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * ClassName: SRBGlobalExceptionHandler
 * Package: com.atguigu.srb.handler
 * Description:
 *
 * @Author 刘强
 * @Create 2024/4/15 19:19
 * @Version 1.0
 */
@RestControllerAdvice
@Slf4j
public class SRBGlobalExceptionHandler {
    @ExceptionHandler(SRBException.class)  // 自定义异常 的处理器
    public ResponseVo srbExceptionHandler(SRBException exception){
        // 打印日志，记录异常的堆栈信息
        log.error(ExceptionUtils.getStackTrace(exception));
        return ResponseVo.error().code(exception.getCode()).message(exception.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public ResponseVo exceptionHandler(Exception e){
        // 打印日志，记录异常的堆栈信息
        String exceptionMsg = ExceptionUtils.getStackTrace(e);
        log.error(exceptionMsg);
        return ResponseVo.error().message(e.getMessage());
    }
    /**
     *    springmvc在解析请求时可能出现的异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public ResponseVo handleServletException(Exception e) {
        log.error(ExceptionUtils.getStackTrace(e));
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return ResponseVo.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }

}
